﻿using System;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using RedRiver.Framework.XtraCommon.FormHelper.TypeSelection;

namespace RedRiver.Framework.XtraCommon.XtraControls {
    internal class TypeSelectorButtonEdit : ButtonEdit {
        /// <summary>
        /// 基类
        /// </summary>
        private Type _baseType;

        private Type _editType;

        public TypeSelectorButtonEdit() {
            BaseType = typeof (object);
            ButtonClick += TypeSelectorButtonEdit_ButtonClick;
        }

        /// <summary>
        /// 基类
        /// </summary>
        public Type BaseType {
            get { return _baseType; }
            set { _baseType = value; }
        }

        public Type EditType {
            get { return _editType; }
            set {
                _editType = value;
                if (value != null) {
                    EditValue = value.FullName;
                } else {
                    EditValue = null;
                }
            }
        }

        private void TypeSelectorButtonEdit_ButtonClick(object sender, ButtonPressedEventArgs e) {
            var edit = (BaseEdit) sender;
            if (!edit.Properties.ReadOnly && edit.Properties.Enabled) {
                Type type = TypeSelectTool.Select(BaseType);
                if (type != null) {
                    EditType = type;
                }
            }
        }
    }
}